/*
   Copyright (C) 2011 Roman Belov <romblv@gmail.com>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include "sim/args.h"
#include "lang/lexer.h"
#include "sim/edsim.h"
#include "util/list.h"
#include "util/link.h"
#include "util/rand.h"

struct edsim_args		args;

static void
edsim_usage()
{
	printf(	"\n"
		"Usage: edsim [-s id] [-l ...] [--] file.edl			\n"
		"								\n"
		"  -s id        - Simulation module ID				\n"
		"  -t number    - Number of the simulation time slices		\n"
		"  -l path      - Additional path to the library		\n"
		"  -p number    - Limit of the propagation passes	 	\n"
		"\n" );
}

int main(int argc, char *argv[])
{
	int		opt;
	const char	*in_file;

	/* Default option values.
	 * */

	args.sim_id		= "sim";
	args.sim_limit		= 0;
	args.pro_limit		= 400;

	rand_seed = time(NULL);

	list_up(&lexglob.path_list);

	/* Default path list to find files.
	 * */

	list_insert(&lexglob.path_list, link_node_alloc("."));
	list_insert(&lexglob.path_list, link_node_alloc("../lang"));

	while ((opt = getopt(argc, argv, "hs:t:l:p:")) != -1) {

		switch (opt) {

			case 's':
				args.sim_id = strdup(optarg);
				break;

			case 't':
				args.sim_limit = strtoul(optarg, NULL, 10);
				break;

			case 'l':
				list_insert(&lexglob.path_list,
						link_node_alloc(strdup(optarg)));
				break;

			case 'p':
				args.pro_limit = strtoul(optarg, NULL, 10);
				break;

			case 'h':
			default:
				edsim_usage();
				exit(EXIT_FAILURE);
		}
	}

	if (argc <= optind) {
		fprintf(stderr, "No input files \n");
		exit(EXIT_FAILURE);
	}

	in_file = argv[optind];

	edsim_simulate(in_file);

	return 0;
}

